package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.DepartmentMapper;
import com.hqyj.dao.PermissionMapper;
import com.hqyj.pojo.Department;
import com.hqyj.pojo.DepartmentPermission;
import com.hqyj.dao.DepartmentPermissionMapper;
import com.hqyj.pojo.Permission;
import com.hqyj.service.IDepartmentPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户_权限表 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-21
 */
@Service
public class DepartmentPermissionServiceImpl implements IDepartmentPermissionService {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired(required = false)
    DepartmentPermissionMapper departmentPermissionMapper;
    @Autowired(required = false)
    PermissionMapper permissionMapper;
    @Autowired(required = false)
    DepartmentMapper departmentMapper;

    @Override
    public HashMap<String, Object> queryAll(DepartmentPermission departmentPermission) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //查询所有权限
        List<Permission> permissionList = permissionMapper.selectList(null);
        //查询所有部门
        List<Department> departmentList = departmentMapper.selectList(null);
        map.put("permissionList",permissionList);
        map.put("departmentList",departmentList);
        //定义分页对象
        Page<DepartmentPermission> page = new Page<>(departmentPermission.getPage(), departmentPermission.getRow());

        /*-------------mybatit plus的分页条件查询 -------------- */
        //查询分页
        QueryWrapper<DepartmentPermission> queryWrapper = new QueryWrapper<>();
        //编号查询
        queryWrapper.eq(departmentPermission.getUserPermissionId() != null,
                "user_permission_id", departmentPermission.getUserPermissionId());
        //用户名模糊查询
        queryWrapper.eq(!StringUtils.isEmpty(departmentPermission.getDepartmentId())
                        && departmentPermission.getDepartmentId()!= null,
                "department_id", departmentPermission.getDepartmentId());
        queryWrapper.eq(!StringUtils.isEmpty(departmentPermission.getPermissionId())
                        && departmentPermission.getPermissionId()!= null,
                "permission_id", departmentPermission.getPermissionId());


        /*-------------自定义的的分页条件查询 -------------- */
        //Page<Permission> aList = permissionMapeer.selectByCon(page,permission);
        //定义当前页的建名
        String curkey = "admin:list:" + departmentPermission.getPage() + ":" + departmentPermission.getRow();
        //定义总页数的建名
        String totalPageKey = "admin:totalPage:" + departmentPermission.getPage() + ":" + departmentPermission.getRow();
        //定义总条数
        String totalKey = "admin:total";
        //定义当前页
        String curPageKey = "admin:curPage:" + departmentPermission.getPage() + ":" + departmentPermission.getRow();
        //定义上一页
        String prePageKey = "admin:prePage:" + departmentPermission.getPage() + ":" + departmentPermission.getRow();
        //定义当前页
        String nextPageKey = "admin:nextPage:" + departmentPermission.getPage() + ":" + departmentPermission.getRow();
        //判断redis是否有缓存
        Object obj = redisTemplate.opsForValue().get(curkey);
        if (obj != null) {
            //当前页集合
            map.put("list", redisTemplate.opsForValue().get(curkey));
            //总页数
            map.put("totalPage", redisTemplate.opsForValue().get(totalPageKey));
            Integer totalPage = (Integer) redisTemplate.opsForValue().get(totalPageKey);
            //总条数
            map.put("total", redisTemplate.opsForValue().get(totalKey));
            //当前页
            map.put("curPage", departmentPermission.getPage());

            if (departmentPermission.getPage() == 1) {
                //上一页
                map.put("prePage", 1);
            } else {
                map.put("prePage", redisTemplate.opsForValue().get(prePageKey));
            }

            if (departmentPermission.getPage() == totalPage) {
                //下一页
                map.put("nextPage", totalPage);
            } else {
                map.put("nextPage", departmentPermission.getPage() + 1);
            }

        } else {
            //查询数据库
            Page<DepartmentPermission> aList = departmentPermissionMapper.selectPage(page, queryWrapper);
            //当前页集合
            map.put("list", aList.getRecords());
            //总页数
            map.put("totalPage", aList.getPages());
            //总条数
            map.put("total", aList.getTotal());
            //当前页
            map.put("curPage", departmentPermission.getPage());
            if (departmentPermission.getPage() == 1) {
                //上一页
                map.put("prePage", 1);
                redisTemplate.opsForValue().set(prePageKey, 1, 60, TimeUnit.SECONDS);
            } else {
                map.put("prePage", departmentPermission.getPage() - 1);
                redisTemplate.opsForValue().set(prePageKey, departmentPermission.getPage() - 1, 60, TimeUnit.SECONDS);
            }

            if (departmentPermission.getPage() == aList.getPages()) {
                //下一页
                map.put("nextPage", aList.getPages());

                redisTemplate.opsForValue().set(nextPageKey, aList.getPages(), 60, TimeUnit.SECONDS);
            } else {
                map.put("nextPage", departmentPermission.getPage() + 1);
                redisTemplate.opsForValue().set(nextPageKey, departmentPermission.getPage() + 1, 60, TimeUnit.SECONDS);
            }

            //每页显示条数
            map.put("row", departmentPermission.getRow());
        }
        return map;
    }

    @Override
    public HashMap<String, Object> addDP(DepartmentPermission departmentPermission) {
        HashMap<String, Object> map = new HashMap<>();
        //条件构造器
        QueryWrapper<DepartmentPermission> queryWrapper = new QueryWrapper<>();
        //根据用户名查询条件
        queryWrapper.eq("permission_id", departmentPermission.getPermissionId());
        queryWrapper.eq("department_id", departmentPermission.getDepartmentId());
        //是否重名
        List<DepartmentPermission> list = departmentPermissionMapper.selectList(queryWrapper);

        if (list.size() > 0) {
            map.put("info", "此部门已有此权限！");
        } else {
            int num = departmentPermissionMapper.insert(departmentPermission);
            if (num > 0) {
                map.put("info", "保存成功");
            } else {
                map.put("info", "保存错误");
            }
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        //删除
        int num = departmentPermissionMapper.deleteById(id);
        if(num == 1){
            map.put("info","删除成功");
        }else{
            map.put("info","删除失败");
        }
        return map;
    }
}
